package com.h3c.ptability.controller;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.h3c.ptability.bo.ElectronicSignBO;
import com.h3c.ptability.dto.EditWaitSignTimeDTO;
import com.h3c.ptability.dto.PtWaitSignInfoDTO;
import com.h3c.ptability.dto.electronicSign.PtEsignRecordDTO;
import com.h3c.ptability.service.IPtWaitSignInfoService;
import com.h3c.ptability.support.ResultData;
import com.h3c.ptability.vo.PtWaitSignInfoVO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 控制层
 *
 * @author chenyifan
 * @since 2022-10-20 15:08:02
 */
@Slf4j
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/ptWaitSignInfo")
public class PtWaitSignInfoController {
    /**
     * 服务对象
     */
    private final IPtWaitSignInfoService ptWaitSignInfoService;

    /**
     * 分页查询待签名单
     *
     * @param waitSignInfoDTO
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("pageWaitSignInfo")
    public ResultData<IPage<PtWaitSignInfoVO>> pageWaitSignInfo(PtWaitSignInfoDTO waitSignInfoDTO,
                                                                @RequestParam Integer currentPage,
                                                                @RequestParam Integer pageSize) {
        try {
            return ResultData.data(ptWaitSignInfoService.pageWaitSignInfo(waitSignInfoDTO, currentPage, pageSize));
        } catch (Exception e) {
            log.error("查询失败,原因:{}", JSONUtil.toJsonStr(e));
            return ResultData.fail("查询失败,原因:{}", e.getMessage());
        }
    }

    /**
     * 导入excel保存待发起签署的信息
     *
     * @param file
     * @return
     */
    @PostMapping("/parseFileAndSaveWaitSign")
    public ResultData<String> parseFileAndSaveWaitSign(@RequestParam MultipartFile file,
                                                       @RequestParam(required = false) String sceneId,
                                                       @RequestParam(required = false) String appId) {
        try {
            ptWaitSignInfoService.parseFileAndSaveWaitSign(file, sceneId, appId);
            return ResultData.success("导入成功");
        } catch (Exception e) {
            log.error("导入失败,原因:{}", JSONUtil.toJsonStr(e));
            return ResultData.fail("导入失败,原因:{}", e.getMessage());
        }
    }


    /**
     * 修改开始和结束签署时间
     *
     * @param editWaitSignTimeDTO
     * @return
     */
    @PostMapping("/editWaitSignTime")
    public ResultData<String> editWaitSignTime(@RequestBody EditWaitSignTimeDTO editWaitSignTimeDTO) {
        try {
            ptWaitSignInfoService.editWaitSignTime(editWaitSignTimeDTO);
            return ResultData.success("修改成功");
        } catch (Exception e) {
            log.error("修改失败,原因:{}", JSONUtil.toJsonStr(e));
            return ResultData.fail("修改失败,原因:{}", e.getMessage());
        }
    }


    /**
     * 删除待签署人员
     *
     * @param id
     * @return
     */
    @GetMapping("/deleteWaitSign")
    public ResultData<String> deleteWaitSign(@RequestParam String id) {
        try {
            ptWaitSignInfoService.removeById(id);
            return ResultData.success("删除成功");
        } catch (Exception e) {
            log.error("删除失败,原因:{}", JSONUtil.toJsonStr(e));
            return ResultData.fail("删除失败,原因:{}", e.getMessage());
        }
    }


    /**
     * 判断是否在名单中
     *
     * @param workerId  员工id
     * @param sceneType 场景类型
     * @return
     */
    @GetMapping("/judgeIfWaitSign")
    public ResultData<Boolean> judgeIfWaitSign(@RequestParam String workerId,
                                               @RequestParam String sceneType) {
        try {
            return ResultData.data(ptWaitSignInfoService.judgeIfWaitSign(workerId, sceneType));
        } catch (Exception e) {
//            log.error("工号:{}判断不通过,原因:{}", workerId, JSONUtil.toJsonStr(e));
            return ResultData.fail(e.getMessage());
        }
    }

    /**
     * 导入名单的待签署的发起签署
     *
     * @param electronicSignBO
     * @return
     */
    @PostMapping("/signWaitForMan")
    public ResultData<String> signWaitForMan(@RequestParam Boolean isRetry,
                                             @RequestBody ElectronicSignBO electronicSignBO) {
        try {
            ptWaitSignInfoService.signWaitForMan(electronicSignBO, isRetry);
            return ResultData.success("电子签章流程发起成功");
        } catch (Exception e) {
            log.error("电子签发起失败:{}", e.getMessage());
            return ResultData.fail(e.getMessage());
        }
    }

    /**
     * 使用导入名单的信息对待签署的发起签署
     *
     * @return
     */
    @PostMapping("/signExcelInfoWaitForMan")
    public ResultData<String> signExcelInfoWaitForMan(@RequestBody List<String> idList,
                                                      @RequestParam String appId) {
        try {
            ptWaitSignInfoService.signExcelInfoWaitForMan(idList, appId);
            return ResultData.success("电子签章流程发起成功");
        } catch (Exception e) {
            log.error("电子签发起失败:{}", e.getMessage());
            return ResultData.fail(e.getMessage());
        }
    }

    /**
     * 导出签署记录
     *
     * @param waitSignInfoDTO
     * @param response
     * @return
     */
    @PostMapping(value = "/downloadRecord")
    public ResultData<String> downloadRecord(@RequestBody PtWaitSignInfoDTO waitSignInfoDTO,
                                             HttpServletResponse response) {
        try {
            ptWaitSignInfoService.downloadRecord(waitSignInfoDTO, response);
            return null;
        } catch (Exception e) {
            log.error("下载失败:{}", e.getMessage());
            return ResultData.fail("下载失败:{}", e.getMessage());
        }
    }

}

